﻿using Newtonsoft.Json;
using System.Net.Http;
using System.Threading.Tasks;

namespace BookApi.Services
{
    public class ApiService : IApiService
    {
        private readonly IHttpClientFactory _httpClientFactory;

        public ApiService(IHttpClientFactory httpClientFactory)
        {
            _httpClientFactory = httpClientFactory;
        }

        public async Task<TResult> GetAsync<TResult>(string url)
        {
            var response = await _httpClientFactory.CreateClient().GetStringAsync(url);

            var result = JsonConvert.DeserializeObject<TResult>(response);
            return result;
        }
    }
}
